package com.winit.service.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import brave.Tracing;
import brave.sampler.Sampler;
import zipkin2.Span;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.Sender;
import zipkin2.reporter.okhttp3.OkHttpSender;

@Configuration
public class TracingConfig {

    /** Configuration for how to send spans to Zipkin */
    @Bean 
    public Sender sender() {
      return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans");
    }

    /** Configuration for how to buffer spans into messages for Zipkin */
    @Bean 
    public AsyncReporter<Span> spanReporter() {
      return AsyncReporter.create(sender());
    }

    /** Controls aspects of tracing such as the name that shows up in the UI */
    @Bean 
    public Tracing tracing(@Value("${spring.application.name}") String serviceName) {
      return Tracing.newBuilder()
          .localServiceName(serviceName)
          .spanReporter(spanReporter()).build();
      // .sampler(Sampler.create(0.01f))
    }
}
